﻿using System;
using System.IO;
using ch8.demo1;
using Console = System.Console;

namespace ch8
{
    class Program
    {
        public static int Gender => 20;

        static void Main(string[] args)
        {
            Ver4("李四", 30);
            Ver4(name: "张三", age: 18);
            Ver4(age: 20, name: "王五");
            Ver4("小刘");
            Ver4("小刘", parent: "老刘");

            Ver6("张三", 9);


            var (name, _) = Ver7("张三");
            Console.WriteLine(name);

            Console.WriteLine("Hello World!");
        }


        static void Ver4(string name, int age = 18, string parent = "张三")
        {
            Console.WriteLine($"{name} now {age} years old and his parent is {parent}!");
        }

        static void Ver6(string name, int age = 18) => Console.WriteLine($"{name} now {age,2} years old {nameof(Sample)}!");

        static (string Name, int Age) Ver7(object obj)
        {
            //if (obj is string name)
            //{
            //    return (name, 19);
            //}

            var typecode = Type.GetTypeCode(obj.GetType());
            var name = typecode switch
            {
                TypeCode.Boolean => Convert.ToBoolean(obj).ToString(),
                TypeCode.Empty => throw new NotImplementedException(),
                TypeCode.Object => throw new NotImplementedException(),
                TypeCode.DBNull => throw new NotImplementedException(),
                TypeCode.Char => throw new NotImplementedException(),
                TypeCode.SByte or TypeCode.Byte or TypeCode.Int16 or TypeCode.UInt16 or TypeCode.Int32 or TypeCode.UInt32 or TypeCode.Int64 or TypeCode.UInt64 => throw new NotImplementedException(),
                TypeCode.Single => throw new NotImplementedException(),
                TypeCode.Double => throw new NotImplementedException(),
                TypeCode.Decimal => throw new NotImplementedException(),
                TypeCode.DateTime => throw new NotImplementedException(),
                TypeCode.String => throw new NotImplementedException(),
                _ => obj as string
            };

            int age = 0;
            if (age == default)
            {
                age = 12_3321_1234;
            }

            if (int.TryParse("123", out int now))
            {
                //do something
            }

            return ("", 0);
        }

        static void Ver8()
        {
            IDemo2 demo = new Demo();
            demo.Start();

            using var fs = new FileStream("", FileMode.Open);
            using var ms = new MemoryStream();

#nullable enable
            string? str = "hello world";
            var result = str?.Substring(str?.IndexOf("wo") ?? 0, 3);
            var result2 = str?[6..^1] ?? "";

            str ??= "hello";

#nullable disable
        }

        static void Ver9()
        {
            var record = new DemoRecord("张三", 18);
            var record1 = new DemoRecord("张三", 18);
            if (record == record1)
            {
                //true
            }

            var record2 = record with { Age = 20 };
            if (record2 == record1)
            {
                //false
            }

            //demo 和record 此时意义和用法相同
            var demo = new DemoClass { Name = "张三", Age = 18 };
        }
    }

    interface IDemo
    {
        void Play();
        void Stop()
        {
            Console.WriteLine("Stoped");
        }

        void Start()
        {
            Stop();
        }
    }
    interface IDemo2
    {
        void Start()
        {
            Console.WriteLine("Start");
        }
    }

    class Demo : IDemo, IDemo2
    {
        public void Play()
        {
            Console.WriteLine("Play");
        }
    }

    /// <summary>
    /// 记录类
    /// </summary>
    /// <param name="Name"></param>
    /// <param name="Age"></param>
    record DemoRecord(string Name, int Age);

    /// <summary>
    /// 跟记录类同功能的类型
    /// </summary>
    class DemoClass
    {
        public string Name { get; init; }
        public int Age { get; init; }
    }
}
